package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zfj
 * @date 2024/4/7 -14:21
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
	@Autowired
	private HospitalService hospitalService;
	@Autowired
	private HospitalSetService hospitalSetService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private ScheduleService scheduleService;

	@ApiOperation(value = "上传排班")
	@PostMapping("/saveSchedule")
	public Result<?> saveSchedule(HttpServletRequest request) {
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
		scheduleService.save(paramMap);
		return Result.ok();
	}

	@ApiOperation(value = "获取排班分页列表")
	@PostMapping("/schedule/list")
	public Result<Page<Schedule>> schedule(HttpServletRequest request) {
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
		// 必须参数校验 略
		String hoscode = (String) paramMap.get("hoscode");
		// 非必填
		String depcode = (String) paramMap.get("depcode");
		int page = StringUtils.isEmpty((CharSequence) paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
		int limit = StringUtils.isEmpty((CharSequence) paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
		ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
		scheduleQueryVo.setHoscode(hoscode);
		scheduleQueryVo.setDepcode(depcode);
		Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
		return Result.ok(pageModel);
	}

	@ApiOperation(value = "删除科室")
	@PostMapping("/schedule/remove")
	public Result<?> removeSchedule(HttpServletRequest request) {
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
		// 必须参数校验 略
		String hoscode = (String) paramMap.get("hoscode");
		// 必填
		String hosScheduleId = (String) paramMap.get("hosScheduleId");

		scheduleService.remove(hoscode, hosScheduleId);
		return Result.ok();
	}

	//上传科室接口
	@PostMapping("/saveDepartment")
	public Result saveDepartment(HttpServletRequest request){
		//获取传递过来的科室
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
//		//获取医院系统传递过来的签名
//		String hospSign = (String)paramMap.get("sign");
//		//根据传递的医院编码查询数据库获取签名
//		String hosCode = (String)paramMap.get("hoscode");
//		String signKey=hospitalSetService.getSignKey(hosCode);
//		//加密
//		String signKeyMD5 = MD5.encrypt(signKey);
//		//判断签名是否一致
//		if(!hospSign.equals(signKeyMD5)){
//			throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//		}
		departmentService.save(paramMap);
		return Result.ok();
	}

	@ApiOperation(value = "获取科室分页列表")
	@PostMapping("/department/list")
	public Result department(HttpServletRequest request) {
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
		// 必须参数校验 略
		String hoscode = (String) paramMap.get("hoscode");
		// 非必填
		String depcode = (String) paramMap.get("depcode");
		int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
		int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
		//TODO
		DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
		departmentQueryVo.setHoscode(hoscode);
		departmentQueryVo.setDepcode(depcode);
		Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
		return Result.ok(pageModel);
	}

	@ApiOperation(value = "删除科室")
	@PostMapping("/department/remove")
	public Result<?> removeDepartment(HttpServletRequest request) {
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
		// 必须参数校验 略
		String hoscode = (String) paramMap.get("hoscode");
		// 必填
		String depcode = (String) paramMap.get("depcode");

		departmentService.remove(hoscode, depcode);
		return Result.ok();
	}

	//查询医院
	@PostMapping("/hospital/show")
	public Result getHospital(HttpServletRequest request){
		//获取传递过来的医院信息
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
		//获取医院编号
		String hoscode = (String) paramMap.get("hoscode");
		//service查询
		Hospital hospital=hospitalService.getByHoscode(hoscode);
		return Result.ok(hospital);
	}
	//上传医院接口
	@PostMapping("/saveHospital")
	public Result saveHosp(HttpServletRequest request){
		//获取传递过来的医院信息
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
		//获取医院系统传递过来的签名
//		String hospSign = (String)paramMap.get("sign");
////		//根据传递的医院编码查询数据库获取签名
////		String hosCode = (String)paramMap.get("hoscode");
////		String signKey=hospitalSetService.getSignKey(hosCode);
////		//加密
////		String signKeyMD5 = MD5.encrypt(signKey);
////		//判断签名是否一致
////		if(!hospSign.equals(signKeyMD5)){
////			throw new YyghException(ResultCodeEnum.SIGN_ERROR);
////		}
		//传输过程中“+”转换为了“ ”，因此我们要转换回来
		String logoData = (String)paramMap.get("logoData");
		logoData = logoData.replaceAll(" ","+");
		paramMap.put("logoData",logoData);
		//调用service方法添加数据库
		hospitalService.save(paramMap);
		return Result.ok();

	}
}
